package com.app.qbake.module.square.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wind.base.adapter.BaseRecyclerAdapter;
import com.wind.data.mine.bean.Address;
import com.app.qbake.R;
import com.app.qbake.module.mine.activity.EditAddressActivity;
import com.app.qbake.module.square.event.AddressItemClickEvent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectAddressAdapter extends BaseRecyclerAdapter<Address, SelectAddressAdapter.ViewHolder> {


    public SelectAddressAdapter(Activity activity, int layoutRes) {
        super(activity, layoutRes);
    }

    @Override
    public ViewHolder onCreateViewHolder(View v) {
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Address address = getItem(position);
        String namePhone=address.getName()+" "+address.getPhone();
        holder.tv_name_phone.setText(namePhone);

        final StringBuilder addrBuilder=new StringBuilder();
        addrBuilder.append(address.getProvince())
                .append(address.getCity())
                .append(address.getArea())
                .append(address.getAddress());
        holder.tv_addr.setText(addrBuilder.toString());

        holder.iv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditAddressActivity.start(mActivity,address);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new AddressItemClickEvent(address));
            }
        });

        if (address.isSelected()){
            holder.iv_cb.setActivated(true);
        }else {
            holder.iv_cb.setActivated(false);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cb)
        ImageView iv_cb;
        @BindView(R.id.tv_name_phone)
        TextView tv_name_phone;
        @BindView(R.id.tv_addr)
        TextView tv_addr;
        @BindView(R.id.iv_edit)
        ImageView iv_edit;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
